<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\unit\controller;

use think\Db;
use think\Cache;
use think\Hook;

class Order extends Base
{
    protected $id = 'order_id';
	protected $table = 'order';
	protected $joins = [
	    ['passenger p','p.passenger_id = m.passenger_id','left'],
        ['company cu','cu.company_id = m.company_id','left'],
        ['order_gps og','og.order_id = m.order_id','left'],
        ['order_fee of','of.order_id = m.order_id','left'],
        ['car_type ct','ct.car_type_id = m.car_type_id','left']
    ];
	protected $field = '*';
	
	public function CarTypes() {
		
		$id = $this->company_id;
		$car_types = Db::name('CarType')->alias('ct')->where('car_type_id','IN',function($query) use($id){
   			 $query->name('Order')->alias('o')->join('car c','c.car_id = o.car_id')->join('passenger p','p.passenger_id = o.passenger_id')->where('p.company_id',$id)->distinct(true)->field('c.car_type_id');
		})->column('car_type_name','car_type_id');

		return $this->apiout($car_types);
		
	}

    //派车
    public function dispatch() {


        if(request()->isPost()){

            $data = [];
            $data['driver_id'] = input('post.driver_id', '', 'intval');
            $data['car_id'] = input('post.car_id', '', 'intval');
            $data['order_state'] = input('post.order_state', '', 'intval');

            $oid = input('param.order_id', '', 'intval');

            $err = null;
            if(!(int)$data['driver_id']){
                $err = '请选择司机';
                return $this->res(false, $err);
            }

            if(!(int)$data['car_id']){
                $err = '请选择车辆';
                return $this->res(false, $err);
            }

            if(!(int)$oid){
                $err = '非法操作';
                return $this->res(false, $err);
            }

//			Hook::listen('send_order',$oid);
            $rslt = Db::name('order')->where('order_id',$oid)->update($data);
            $rslt && Hook::listen('send_order',$oid);

            return $this->res($rslt,'',url('order/details',['order_id'=>$oid]));

        }

        return $this->details();
    }
	
	
}
